package net.xt.testthread.mapper;

import net.xt.testthread.po.TestPO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TestMapper {

    void _batchInsert(@Param("tableName") String tableName, @Param("list") List<TestPO> list);

    default void batchInsert(String tableName, List<TestPO> list) {
        // 每次处理1000条数据
        int batchSize = 1000;

        // 根据总记录数，判断要循环处理多少次（总记录数/每次要处理的记录数，能整除就取商值，不能整除就取商值+1）
        int size = list.size();
        int batch = size % batchSize == 0 ? size / batchSize : size / batchSize + 1;
        for (int i = 0; i < batch; i++) {
            int fromIndex = i * batchSize;
            int toIndex = fromIndex + batchSize;
            if (toIndex > size) {
                toIndex = size;
            }
            List<TestPO> insertList = list.subList(fromIndex, toIndex);
            this._batchInsert(tableName, insertList);
        }
    }

    default boolean batchInsert2(String tableName, List<TestPO> list) {
        // 每次处理1000条数据
        int batchSize = 1000;

        // 根据总记录数，判断要循环处理多少次（总记录数/每次要处理的记录数，能整除就取商值，不能整除就取商值+1）
        int size = list.size();
        int batch = size % batchSize == 0 ? size / batchSize : size / batchSize + 1;
        for (int i = 0; i < batch; i++) {
            int fromIndex = i * batchSize;
            int toIndex = fromIndex + batchSize;
            if (toIndex > size) {
                toIndex = size;
            }
            List<TestPO> insertList = list.subList(fromIndex, toIndex);
            this._batchInsert(tableName, insertList);
        }
        return true;
    }


    @Delete("delete from ${tableName}")
    void delete(@Param("tableName") String tableName);

    @Select("select goods_count from t_storein where goods_id = #{goodsId}")
    public Integer getStoreinNum(@Param("goodsId") Integer goodsId);

    @Update("update t_storein set goods_count = goods_count - 1 where goods_id = #{goodsId}")
    void updateStoreinNum(@Param("goodsId") Integer goodsId);

}